var util = require('../../utils/util.js')
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var reload = true;
var page = 2;
var hasMore = true;
var loadingMore = false;

var SDKVersion = '';
var status = true;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: app.globalData.imgDomain,

    classlysData: '',

    zxsData: [],
    hasData: true,
    util: util,
    uid: 0,
    token: '',
    openid: '',
    canEdit: 0,

    bottom_nav_data: [],
    isIphone_x: false,
    nav_bgColor: '#ffffff',

    shouqian_phone: '',
    colorSet: '',
    wz_item_img_w_1: 300,

    tabCataData: [{ id: 0, title: "进驻", subList: [] },
    { id: 1, title: "打扫", subList: [] }],
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var tc = app.get_tc();
    var titlename = tc == 1 ? '留言管理' : '留言管理';
    wx.setNavigationBarTitle({
      title: titlename,
    });
    this.setData({
      windowH: wx.getSystemInfoSync().windowHeight,
    })

    page = 2;
    hasMore = true;
    loadingMore = false;

    var that = this;

    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color,
      })
      that.resetNavColor();
    });

    //初始化底部导航
    var b_nav_data = app.getBottomNavData('car-commerce/car-commerce', function (data) {
      that.setData({
        bottom_nav_data: data,
        nav_bgColor: data[0].bgcolor,
      });
    });

    wx.getSystemInfo({
      success: function (res) {
        SDKVersion = res.SDKVersion;
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          tc: app.get_tc(),
          isIphone_x: isIphone_x,
          wz_item_img_w_1: res.windowWidth - 25
        });
      }
    })

    reload = true;
    //当前登录用户信息
    app.LoginUserInfo(function (res) {
      var authFail = res == -1;//用户拒绝授权
      if (!authFail) {
        //更新数据
        that.setData({
          uid: res.uid,
          token: res.token,
          openid: res.wxsmall_openid
        });
      }

    });
    //获取关于等信息
    app.getAllAbout('shouqian', function (res) {
      that.setData({
        shouqian_phone: res
      })
    });
    // that.getOrgVerified();
    that.getLiuyan();
  },


  //获取留言信息
  getLiuyan: function () {
    var that = this;
    var params = {
      publickey: util.publiccode,
      ac: "getliuyan",
      uid: that.data.uid,
      orgid: util.orgid,
    }
    util.api({
      data: params,
      success: function (responseData) {
        console.log("留言数据：", responseData.data);
        var result = []
        if (responseData.data.res==false){
          
        } else {
          result = responseData.data;
          for (var i = 0; i < result.length; i++) {

            result[i]['post_time'] = that.toDate(result[i]['post_time']);

          }
          that.setData({
            liuyanArray: result
          })
          if (util.orgid == 18535) {
            that.setData({
              liuyanArraySource: result
            })
          }
        }
      }
    });
  },

  huifang: function (e) {
    console.log(e);
    var id = e.currentTarget.dataset.id;
    var state = e.currentTarget.dataset.state;
    var that = this;
    var privatekey = utilMd5.hexMD5(that.data.uid + 'editliuyan' + that.data.token);
    util.api({
      method: "POST",
      data: {
        ac: "editliuyan",
        uid: that.data.uid,
        id: id,
        state: state,
        orgid: util.orgid,
        privatekey: privatekey
      },
      success: function (responseData) {
        console.log(responseData);
        if (responseData.data.res == true) {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 3000,
          })
          wx.redirectTo({ url:"/pages/liuyan-center/liuyan-center"})
        } else {
          wx.showToast({
            title: '提交失败',
            icon: 'success',
            duration: 3000,
          })
        }

      }
    });
  },


  //点击图片放大
  showEWM: function (e) {
    var shareImgUrl = e.currentTarget.dataset.imgsrc;
    wx.previewImage({
      current: shareImgUrl,
      urls: [shareImgUrl]
    })
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  goAtherPage: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.navigateTo({ url: url })
  },
  goAtherPageRedirect: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.redirectTo({ url: url })
  },

  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);

    var path = e.detail.value.nav_path;
    if (path) {
      wx.redirectTo({ url: path })
    }
  },

  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';
    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    })
  },

  //时间戳转换时间  
  toDate: function (number) {
    var n = number * 1000;
    var date = new Date(n);
    var Y = date.getFullYear() + '.';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '.';
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    return (Y + M + D)
  },

  makeCallPhone: function (e) {
    // app.saveFormId(e.detail.formId);
    var phoneNum = e.currentTarget.dataset.number;
    console.log("phoneNum:" + phoneNum);
    wx.makePhoneCall({
      phoneNumber: phoneNum,
      success: function (e) {
        console.log("make call success:", JSON.stringify(e));
      },
      fail: function (e) {
        console.log("make call fail:", JSON.stringify(e));
      }
    })
  },

  getOrgVerified: function () {
    var that = this;
    
    var params = {
      publickey: util.publiccode,
      ac: "getOrgVerified",
      uid: that.data.uid,
      orgid: util.orgid,
    }
    util.api({
      data: params,
      success: function (responseData) {
        console.log("getOrgVerified：", responseData.data);

        var result = responseData.data;
        if(result.res==true){

          that.getLiuyan();
        }
      }
    });
  },


  changeTabCata: function (e) {
    console.log("Tabcurrcata", e.currentTarget.dataset.current)
    var cur = e.currentTarget.dataset.current;
    if (this.data.tabcurrcata == cur) {
      return false;
    } else {
      this.setData({
        tabcurrcata: cur
      })
      var that = this;
      that.data.tabCataData[that.data.tabcurrcata].subList = [];
    }


    var liuyanArraySource = [].concat(that.data.liuyanArraySource);
    var liuyanArray = that.data.liuyanArraySource;
    for (var i = 0; i < liuyanArray.length; i++) {
      if (this.data.tabcurrcata == 0) {
        if (liuyanArray[i].message.indexOf("服务")>=0) {
          liuyanArray.splice(i, 1);
          i--
        }
      } else if (this.data.tabcurrcata == 1) {
        if (liuyanArray[i].message.indexOf("加入")>=0) {
          liuyanArray.splice(i, 1);
          i--
        }
      }
    }
    this.setData({
      liuyanArraySource: liuyanArraySource,
      liuyanArray: liuyanArray
    })

  },

})